/**
 * Andrew Loiacono
 * Cosc455, Josh Dehlinger
 * andrews-first-compiler
 */

package edu.towson.cosc.cosc455.aloiac1.markdowncompiler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;

public class Compiler {

	/** Initializations */
	
	/** Determines if writing to a .html file is necessary */
	static boolean canWrite = false;
	static String token = "";
	static String textFromFile = "";
	/** Stack of all markdown tokens */
	static Stack<String> completeTokens = new Stack<String>();
	/** Stack of all html tokens */
	static Stack<String> htmlStack = new Stack<String>();
	/**
	 * define[] is the array to hold any define notations
	 * 
	 * defineNewIndex is used to determine the index to put in new DEFINE value
	 */
	static String define[] = new String[100];
	static int defineNewIndex = 0;
	/**
	 * markdown[] is used for the lookup() function in the SyntaxAnalyzer
	 * and the convert() function in the SemanticAnalyzer
	 * 
	 * html[] is used for the convert() function in the SemanticAnalyzer
	 * 
	 * the indexes of both are perfectly aligned together for easy conversion
	 */
	static String markdown[] = new String[] {"#START DOCUMENT","#END DOCUMENT","#START HEAD","#END HEAD","#START TITLE","#END TITLE",
			"#START PARAGRAPH","#END PARAGRAPH","#START BOLD","#END BOLD","#START ITALICS","#END ITALICS","#START LIST","#END LIST","#START ITEM",
			"#END ITEM","#START AUDIO","#END AUDIO","#START VIDEO","#END VIDEO","#NEWLINE","#START USE","#END USE","#START DEFINE","#END DEFINE",
			"#VALUE","#NAME"};
	static String html[] = new String[] {"<html>","</html>","<head>","</head>","<title>","</title>","<p>","</p>","<b>","</b>","<i>",
			"</i>","<ul>","</ul>","<li>","</li>","<audio controls><source src=\"","\"></audio>","<iframe src=\"","\"/>","<br>"}; 
	static MySyntaxAnalyzer syntax = new MySyntaxAnalyzer();
	static MySemanticAnalyzer semantic = new MySemanticAnalyzer();
	
	public static void main(String[] args) throws IOException {
		String fileName = args[0];
		
		/** Only files with .mkd will be parsed */
		if (!fileName.endsWith(".mkd")) {
			System.out.println("This program only works with the .mkd file type!");
			System.exit(0);
		}
		
		try {
			File file = new File(fileName);
			Scanner input = new Scanner (file);
			/** .mkd contents are going to a String called textFromFile */
			while (input.hasNextLine())
				textFromFile = textFromFile + input.nextLine();
			input.close();
			try {
				/**
				 * Start of the parser
				 * Lexical Analyzer is called in document() to obtain first token
				 */
				syntax.document();
			} catch (CompilerException e) {
				e.printStackTrace();
			}
			/** Semantic call */
			semantic.toHTML();
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		/** Call to change fileName */
		fileName=changeFileName(fileName);
		/** Call to write to .html file */
		writeToFile(fileName);
	}
	
	/**
	 * Checks to see if new DEFINE variable name is already located in the array
	 * If so it determines where in the array and sets the index
	 */
	public static boolean inDefine() {
		for (int i=0; i<define.length; i=i+2) {
			if (token.equalsIgnoreCase(define[i])) {
				defineNewIndex=i;
				return true;
			}
		}
		return false;
	}
	/** Checks for any remaining characters in textFromFile. It ignores spaces */
	public static boolean checkForCharacters() {
		for (int i=0; i < textFromFile.length(); i++) {
			if (textFromFile.charAt(i) ==' ' || textFromFile.charAt(i) =='\t' || textFromFile.charAt(i) =='\n' || textFromFile.charAt(i) =='\r') {}
			else
				return true;
		}
		return false;
	}
	
	/** Changes fileName from .mkd to .html */
	public static String changeFileName(String fileName) {
		String newfileName="";
		for (int i=0; i<fileName.length(); i++) {
			if (fileName.charAt(i)=='.') {
				newfileName=newfileName+fileName.charAt(i);
				if (fileName.charAt(i+1)=='m') 
					if (fileName.charAt(i+2)=='k') 
						if (fileName.charAt(i+3)=='d')
							i=fileName.length();
			}
			else
				newfileName=newfileName+fileName.charAt(i);
		}
		return newfileName + "html";
	}
	
	/** Outputs to a html file */
	public static void writeToFile(String fileName) throws IOException {
		if (canWrite) {
			FileWriter writer = new FileWriter(fileName); 
			while(!htmlStack.isEmpty()) {
				writer.write(htmlStack.pop());
			}
			writer.close();
		}
		else
			return;
	}
}
